export function formatDate(time, fmt) {
  let date = new Date(time);

  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(
      RegExp.$1,
      (date.getFullYear() + '').substr(4 - RegExp.$1.length)
    );
  }
  let o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds(),
  };
  for (let k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      let str = o[k] + '';
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length === 1 ? str : padLeftZero(str)
      );
    }
  }
  return fmt;
}

function padLeftZero(str) {
  return ('00' + str).substr(str.length);
}

export function formatMonthDay(time) {
  return formatDate(time, 'MM月dd日');
}

export function formatMinuteSecond(time) {
  return formatDate(time, 'mm:ss');
}

export function throttle(fn, delay) {
  let pre = 0;
  return (...args) => {
    let now = new Date();
    if (now - pre > delay) {
      fn.apply(this, args);
    }
    pre = now;
  };
}

export function debounce(fn, delay, isImmediately = true) {
  let timer = null;
  let Immediately = isImmediately
  return (...args) => {
      if (timer) clearTimeout(timer);
      Immediately ? fn.apply(this, args) : timer = setTimeout(() => fn.apply(this, args), delay);
      Immediately = !isImmediately
  }
}

export function getSizeImage(imgUrl, size) {
  return `${imgUrl}?param=${size}x${size}`;
}


export function getPlayUrl(id) {
  return `https://music.163.com/song/media/outer/url?id=${id}.mp3`
}

export function getRandom(num) {
  return Math.floor(Math.random() * num)
}